import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang.StringUtils;

import java.io.File;

/**
 * @ClassName ConfigStrategy
 * @Description T0D0
 * @Author ykq11
 * @Date 2024/2/20 22:15
 * @Version 1.0
 **/
@Slf4j
public class ConfigStrategy {
    private boolean autoReload;
    private static final String BASE_PATH;
    private static final String GLOBAL_CONFIG_PATH = "global.config.path";
    private static final String fileName="conf.properties";
    private static volatile PropertiesConfiguration prop =null;
    static{
        log.info("start init configuration strategy");
        SystemConfiguration sysConfig = new SystemConfiguration();
        String globalPath = sysConfig.getString(GLOBAL_CONFIG_PATH);
        log.info("configuration globalPath {} ", globalPath);
        // 默认加载classpath下面的文件
        if(StringUtils.isBlank(globalPath)){
            globalPath = Thread.currentThread().getContextClassLoader().getResource(fileName).getFile();
        }
        BASE_PATH=globalPath;
    }
    public ConfigStrategy(boolean autoReload) throws ConfigurationException {
        this.autoReload = autoReload;
        loadConfig();
    }
    public void loadConfig() throws ConfigurationException {
        if (null == prop){
            prop = new PropertiesConfiguration();
        }
        File file = new File(BASE_PATH);
        prop.setFile(file);
        prop.setAutoSave(false);
        // 重载策略,5秒钟监视文件变化
        if(autoReload){
            prop.setReloadingStrategy(new FileChangedReloadingStrategy());
        }
        prop.load();
    }
    /**
     * 获取字符串类型的配置项
     *
     * @param key
     * @return value
     */
    public  String getProperty(String key) {
        return prop.getString(key);
    }
    /**
     * 获取整数类型的配置项
     *
     * @param key
     * @return value
     */
    public  Integer getInteger(String key) {
        String value = getProperty(key);
        return Integer.valueOf(value);
    }
    /**
     * 获取布尔类型的配置项
     *
     * @param key
     * @return value
     */
    public  Boolean getBoolean(String key) {
        String value = getProperty(key);
        return Boolean.valueOf(value);
    }
    /**
     * 获取Long类型的配置项
     *
     * @param key
     * @return
     */
    public  Long getLong(String key) {
        String value = getProperty(key);
        return Long.valueOf(value);
    }
    private static class SingletonHelp {
        static ConfigStrategy instance;
        static {
            try {
                instance = new ConfigStrategy(true);
            } catch (ConfigurationException e) {
                log.error("configuration strategy init error ", e);
            }
        }
    }
    public static ConfigStrategy build(){
        return SingletonHelp.instance;
    }
}
